const state = {
  produce: {
    detail: {},
    content: []
  },
  defaultProduce: {
    imagery: [],
    role: [],
    scene: "",
    word: "",
  },
  userProduceList: [],
  communityList: [],
  collectionIdList: [],
  produceCurrent: 0,
  actionElement: {}
}
const getters = {
  getProduce(state) {
    return state.produce
  },
  //当前操作页
  getProduceCurrent(state) {
    return state.produceCurrent
  },
  //当前操作元素
  getActionElement(state) {
    return state.actionElement
  },
  //用户创作列表
  getUserProduceList(state) {
    return state.userProduceList
  },
  //用户收藏的id
  getCollectionIdList(state) {
    return state.collectionIdList
  },
  //社区列表
  getCommunityList(state) {
    return state.communityList
  }
}
const mutations = {
  //初始化创作
  setProduce(state, param) {
    if (JSON.stringify(param) === "{}") {
      state.produce.content.push(JSON.parse(JSON.stringify(state.defaultProduce)))
    }
    else {
      state.produce = param
    }
    state.produceCurrent = 0
    state.setActionElement = {}
  },
  //清空创作
  clearProduce(state) {
    state.produce = {
      detail: {},
      content: [JSON.parse(JSON.stringify(state.defaultProduce))]
    }
    state.produceCurrent = 0
  },
  //设置社区列表
  setCommunityList(state, param) {
    state.communityList = param
  },
  //设置用户收藏的id
  setCollectionIdList(state, param) {
    state.collectionIdList = param
  },
  //当前页
  setProduceCurrent(state, param) {
    state.produceCurrent = param
  },
  //当前操作元素
  setActionElement(state, param) {
    state.actionElement = JSON.parse(JSON.stringify(param))
  },
  //添加意象
  insertProduceImagery(state, param) {
    state.produce.content[state.produceCurrent].imagery.push(param)
  },
  //删除意象
  deleteProduceImagery(state, id) {
    const imageryList = state.produce.content[state.produceCurrent].imagery
    for (let i = 0, len = imageryList.length; i < len; i++) {
      if (imageryList[i].produceEryId === id) {
        imageryList.splice(i, 1)
        return
      }
    }
  },
  //添加角色
  insertProduceRole(state, param) {
    state.produce.content[state.produceCurrent].role.push(param)
  },
  //删除角色
  deleteProduceRole(state, id) {
    const roleList = state.produce.content[state.produceCurrent].role
    for (let i = 0, len = roleList.length; i < len; i++) {
      if (roleList[i].produceRoleId === id) {
        roleList.splice(i, 1)
        return
      }
    }
  },
  //新增创作页
  insertProduce(state) {
    state.produce.content.push(JSON.parse(JSON.stringify(state.defaultProduce)))
  },
  //设置用户创作列表
  setUserProduceList(state, param) {
    state.userProduceList = param
  }
}
const actions = {
  setProduce({ commit }, param) {
    commit("setProduce", param)
  },
  clearProduce({ commit }, param) {
    commit("clearProduce", param)
  },
  setCollectionIdList({ commit }, param) {
    commit("setCollectionIdList", param)
  },
  setCommunityList({ commit }, param) {
    commit("setCommunityList", param)
  },
  setUserProduceList({ commit }, param) {
    commit("setUserProduceList", param)
  },
  setProduceCurrent({ commit }, param) {
    commit("setProduceCurrent", param)
  },
  setActionElement({ commit }, param) {
    commit("setActionElement", param)
  },
  insertProduceImagery({ commit }, param) {
    commit("insertProduceImagery", param)
  },
  deleteProduceImagery({ commit }, id) {
    commit("deleteProduceImagery", id)
  },
  insertProduceRole({ commit }, param) {
    commit("insertProduceRole", param)
  },
  deleteProduceRole({ commit }, id) {
    commit("deleteProduceRole", id)
  },
  insertProduce({ commit }) {
    commit("insertProduce")
  }
}
export default {
  namespaced: true, //用于在全局引用此文件里的方法时标识这一个的文件名
  state,
  getters,
  mutations,
  actions
}
